<template>
  <div>
    <router-link to="/food/AddCategory" v-if="role==2"><el-button>添加</el-button></router-link>
    <el-table
      :data="CategoryList"
      :stripe="true"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号">
      </el-table-column>
      <el-table-column
        prop="name"
        label="分类名">
      </el-table-column>
      <el-table-column
        prop="count"
        label="零食数量">
      </el-table-column>

      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <router-link :to="'/food/FoodList?cid='+scope.row.id"><el-button>查看零食</el-button></router-link>
          <el-button @click="deleteCate(scope.row.id)" v-if="role==2">删除类别</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
    export default {
        name: "CategoryList",
      data(){
          return {
            CategoryList:[],
            role:sessionStorage.getItem("role")
          }
      },
      created(){
        this.selectAll();
      },
      methods:{
        deleteCate(id){
          this.axios.delete("/category/delete/"+id).then((resp)=>{
            this.$message.success("删除成功！");
            this.selectAll();
          });
        },
        selectAll(){
          //发出ajax请求请求后端代码
          this.axios.get("/category/selectAll").then((resp)=>{
            this.CategoryList=resp.data;
          });
        }
      }
    }
</script>

<style scoped>

</style>
